package com.acceptable.qucun.file.controller;


import com.acceptable.qucun.file.entity.CurrentDirectory;
import com.acceptable.qucun.file.entity.FileInfo;
import com.acceptable.qucun.file.service.TemplateFileService;
import com.acceptable.qucun.generic.util.result.success.FileSuccessResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 巴斯罗斯·K·赫斯洛利
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/file")
public class FileRestController {

    @Autowired private TemplateFileService fileService;

    @PostMapping("/upload_exist_check")
    public Map<String, Object> uploadExistCheck(@RequestAttribute("data_map") Map<String, String> dataMap,
                                                HttpServletRequest request){
        return fileService.uploadExistCheck(dataMap, request);
    }

    @PostMapping("/chunk_upload")
    public String fileChunkUpload(@RequestAttribute("data_map") Map<String, String> dataMap,
                                  @RequestPart("raw_chunk_data") byte[] chunkData,
                                  HttpServletRequest request){
        return fileService.chunkUpload(dataMap, chunkData, request);
    }

    @PostMapping("/check_rest_volume")
    public String checkLeftVolume(@RequestAttribute("data_map") Map<String, String> dataMap){
        fileService.checkRestVolume(dataMap);
        return FileSuccessResult.REST_VOLUME_SUCCESS.getCode();
    }

    @PostMapping("/merge")
    public Map<String, Object> mergeFile(@RequestAttribute("data_map") Map<String, String> dataMap,
                                         HttpServletRequest request){
        return fileService.mergeFile(dataMap, request);
    }

    @PostMapping("/info")
    public Map<String, Object> fileInfo(@RequestAttribute("data_map") Map<String, String> dataMap){
        Integer userId = Integer.parseInt(dataMap.get("user_id"));
        Long fileId = Long.parseLong(dataMap.get("file_id"));
        Boolean share = Boolean.parseBoolean(dataMap.get("share_status"));
        FileInfo fileInfo = fileService.selectExistedSingleFileInfo(userId, fileId, share);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", FileSuccessResult.INFO_SELECT_SUCCESS.getCode());
        resMap.put("file_info", fileInfo);
        return resMap;
    }

    @PostMapping("/sub_dict_list")
    public Map<String, Object> subDictInfo(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.selectExistedSingleDictSubDict(Integer.parseInt(dataMap.get("user_id")),
                                                          Long.parseLong(dataMap.get("parent_dict_id")));
    }

    @PostMapping("/enter_dict")
    public Map<String, Object> enterDict(@RequestAttribute("data_map") Map<String, String> dataMap){
        CurrentDirectory dict = fileService.selectExistedSingleDictInfo(
                Integer.parseInt(dataMap.get("user_id")),
                Long.parseLong(dataMap.get("file_id")),
                Boolean.parseBoolean(dataMap.get("share_status")));

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("current_dict", dict);
        resMap.put("code", FileSuccessResult.ENTER_DICT_SUCCESS.getCode());
        return resMap;
    }

    @PostMapping("/create_dict")
    public Map<String, Object> createDict(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.createDict(dataMap);
    }

    @PostMapping("/rename")
    public Map<String, Object> renameFile(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.renameExistedSingleFile(dataMap);
    }

    // 伪删除文件 直接标记文件被删除
    @PostMapping("/logical_delete")
    public String logicalDeleteFile(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.logicalDeleteExistedMultiFile(dataMap);
    }

    @PostMapping("/move")
    public String moveFile(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.moveExistedMultiFile(dataMap);
    }

    @PostMapping("/copy")
    public String copyFile(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.copyExistedMultiFile(dataMap);
    }

    @PostMapping("/download_exist_check")
    public Map<String, Object> fileDownload(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.downloadExistedMultiCheck(dataMap);
    }

    @PostMapping("/entire_download")
    public ResponseEntity<byte[]> fileEntireDownload(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.fileEntireDownload(dataMap);
    }

    @PostMapping("/filter")
    public Map<String, Object> filterCategory(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.filterCategory(dataMap);
    }

    @PostMapping("/logical_recovery")
    public String logicalRecovery(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.logicalRecovery(dataMap);
    }

    @PostMapping("/physical_delete")
    public String physicalDelete(@RequestAttribute("data_map") Map<String, String> dataMap){
        return fileService.physicalDeleteByVerifyCode(dataMap);
    }

}

